package com.sllp.C71S3TsllpShop.biz.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import com.github.pagehelper.PageHelper;
import com.sllp.C71S3TsllpShop.bean.Admin;
import com.sllp.C71S3TsllpShop.bean.AdminExample;
import com.sllp.C71S3TsllpShop.bean.AdminExample.Criteria;
import com.sllp.C71S3TsllpShop.bean.Result;
import com.sllp.C71S3TsllpShop.biz.AdminBiz;
import com.sllp.C71S3TsllpShop.dao.AdminMapper;

@Service
public class AdminBizImpl implements AdminBiz {

	@Resource
	private AdminMapper am;
	
	
	@Override
	public Result adminLogin(Admin admin) {
		/*
		 * 登录需要搜索数据库中是否有此用户名和密码
		 * 若有则需要查看此用户的登录状态是否为在线状态如果为在线状态则不让登录
		 * 否则修改此人登录状态为已登陆
		 * 
		 * 优化点：是否直接搜索未登录的用户会好一些
		 */
		String md5Password = DigestUtils.md5DigestAsHex(admin.getPwd().getBytes());
		AdminExample example = new AdminExample();
		example.createCriteria()
			.andLoginNameEqualTo(admin.getLoginName())
			.andPwdEqualTo(md5Password);
		List<Admin> admins = am.selectByExample(example);
		if(admins.size()>1) {
			// 用户存在重复，要求管理员处理，此处可发送邮件给数据库管理员处理这个情况
			return new Result(0, "管理员账号存在重复现象",admins);
		}else if(admins.size()==0){
			return new Result(0,"无此管理员",admin);
		}
		
		Admin loginAdmin = admins.get(0);
		loginAdmin.setLoginState(1);
		int updateResult = am.updateByPrimaryKey(loginAdmin);
		if(updateResult<0) {
			return new Result(0,"登陆失败",loginAdmin);
		}
		return new Result(1,"登陆成功",loginAdmin);
	}


	@Override
	public Result selectAdminByLoginName(String loginName) {
		AdminExample example = new AdminExample();
		example.createCriteria()
			.andLoginNameEqualTo(loginName);
		List<Admin> admins = am.selectByExample(example);
		if(admins.size()>1) {
			// 用户存在重复，要求管理员处理，此处可发送邮件给数据库管理员处理这个情况
			return new Result(0, "管理员账号存在重复现象",admins);
		}else if(admins.size()==0){
			return new Result(0,"无此管理员");
		}
		return new Result(1, "查询成功", admins.get(0));
		
	}
	
	@Override
	public Result resetAdminPwd(Admin admin) {
		Result result = selectAdminByLoginName(admin.getLoginName());
		if(result.getSucess()==0) {
			return result;
		}
		Admin findAdmin = (Admin)result.getObj();
		if(findAdmin.getPwd().equals(admin.getPwd())) {
			return new Result(0,"新密码不能与旧密码相同");
		}
		AdminExample adminExample = new AdminExample();
		adminExample.createCriteria().andLoginNameEqualTo(admin.getLoginName());
		int updateCode = am.updateByExampleSelective(admin, adminExample);
		if(updateCode>0) {
			return new Result(1,"重置密码成功");
		}
		return new Result(0,"重置密码失败");
	}
	
	@Override
	public Result addAdmin(Admin admin) {
		int code = am.insertSelective(admin);
		if(code>0) {
			return new Result(1, "添加成功");
		}
		return new Result(0, "添加失败");
	}
	
	@Override
	public List<Admin> findAdminByCondition(Admin admin, Integer page, Integer pageSize){
		AdminExample adminExample = new AdminExample();
		if(admin!=null) {
			Criteria criteria = adminExample.createCriteria();
			if(admin.getId()!=null) {
				criteria.andIdEqualTo(admin.getId());
			}
			if(admin.getTrueName()!=null) {
				criteria.andTrueNameLike("%"+admin.getTrueName()+"%");
			}
			if(admin.getState()!=null) {
				criteria.andStateEqualTo(admin.getState());
			}
		}
		List<Admin> adminList = null;
		if(page!=null) {
			PageHelper.startPage(page, pageSize);
			adminList = am.selectByExample(adminExample);
		}else {
			adminList = am.selectByExample(adminExample);
		}
		return adminList;
	}
	
	@Override
	public Result updateAdminState(Admin admin) {
		if(admin.getState()==2) {
			admin.setState(3);
		}else if(admin.getState()==3) {
			admin.setState(2);
		}
		int code = am.updateByPrimaryKeySelective(admin);
		if(code>0) {
			return new Result(1, "修改成功");
		}
		return new Result(0, "修改失败");
	}
	
}
